﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace eBrain
{
    public class Logger
    {
        String LogFileName = "";
        public String Prefix = "";
        public String path = ""; 

        Mutex mtx = new Mutex(); 


        public Logger(String _LogFileName)
        {
            Prefix = DateTime.Now.ToString("[MMdd-HH.mm.ss]");
            LogFileName =  Prefix + "-" + _LogFileName ;
        }


        public Logger(String _LogFileName, String _path)
        {
            Prefix = DateTime.Now.ToString("[MMdd-HH.mm.ss]");
            path = _path + "\\" + DateTime.Now.Year.ToString("0000") + "-" + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00") +"\\";
            LogFileName = path + Prefix + "-" + _LogFileName;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }


        public void Log(String str_to_log)
        {
            try
            {
                mtx.WaitOne();
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(LogFileName, true))
                {
                    DateTime myTime = DateTime.Now;
                    
                    file.WriteLine("[" + myTime.ToString("HH:mm:ss.fff") + "] : "+ str_to_log);
                }
                mtx.ReleaseMutex();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Logger.Log() Error \r\n" + ex.Message +"\r\n" + ex.InnerException.ToString() +"\r\n" + ex.StackTrace);
            }
        }
    }
}